package _022_2_12

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func getGoodAtSplitHeapId(goodHeap [][]int, n int) int {

	for i := 0; i < len(goodHeap); i++ {
		if n <= goodHeap[i][1] && n >= goodHeap[i][0] {
			return i
		}
	}
	return -1
}

func getMaxHeapGood(w []int, seq []int) {
	splitGood := make([][]int, 0)
	splitGood = append(splitGood, w)
	for i := 0; i < len(seq); i++ {
		goodI := seq[i]
		id := getGoodAtSplitHeapId(splitGood, goodI)

		goodHeap := splitGood[id]

		if goodHeap[0] <= goodI-1 {
			splitGood = append(splitGood, []int{goodHeap[0], goodI - 1})
		}

		if goodHeap[1] >= goodI+1 {
			splitGood = append(splitGood, []int{goodI + 1, goodHeap[1]})
		}
		max := 0
		for splitI := 0; splitI < len(splitGood); splitI++ {
			if splitGood[i][0] == splitGood[i][1] {
				if max < w[splitGood[i][0]] {
					max = w[splitGood[i][0]]
				}
			} else {
				temp := 0
				for k := splitGood[i][0]; k < splitGood[i][1]; k++ {
					temp += w[k]
				}
				if temp > max {
					max = temp
				}
			}
		}
		fmt.Println(max)
	}

}

func main() {
	stdinR := bufio.NewReader(os.Stdin)

	line, _, _ := stdinR.ReadLine()

	num, _ := strconv.Atoi(string(line))

	w := make([]int, num)

	weightLineStr, _, _ := stdinR.ReadLine()
	weightsStr := strings.Split(string(weightLineStr), " ")

	for i := 0; i < len(weightsStr); i++ {
		wI, _ := strconv.Atoi(weightsStr[i])
		w[i] = wI
	}

	seqLineStr, _, _ := stdinR.ReadLine()
	fetchSeqStr := strings.Split(string(seqLineStr), " ")

	fetchSeq := make([]int, num)

	for i := 0; i < len(fetchSeqStr); i++ {
		seqI, _ := strconv.Atoi(fetchSeqStr[i])
		fetchSeq[i] = seqI
	}

}
